package divide;

class Solution {
    public int divide(int dividend, int divisor) {
        if (dividend == Integer.MIN_VALUE && divisor == -1) return Integer.MAX_VALUE;
        long divid = (int)dividend;
        long divis = (int)divisor;

        boolean flag = true;
        long temp, result = 1, ans = 0;

        if (divid < 0 && divis > 0) {divid = divid; flag = false;}
        else if (divid > 0 && divis < 0){divis = - divis; flag = false;}
        else if (divid < 0 && divis < 0){divid = divid; divis = -divis;}
        else;

        while (divid - divis >= 0) {
            temp = divis << 1;
            result = 1;
            while (divid - temp >= 0){
                result = result << 1;
                temp = temp << 1;
            }
            divid -= temp >> 1;
            ans += result;
        }
        if (flag) return (int)ans;
        else return (int)-ans;
    }
}
